package com.food.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.food.pojo.Users;
import com.food.result.Result;
import com.food.service.UsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/system/sysRole")
public class AdminUserController {
    @Autowired
    private UsersService userService;

    //    查询所有
    @ApiOperation(value = "查询所有")
    @GetMapping("/findAll")
    public Result findAll() {
        List<Users> list = userService.list();
        return Result.ok(list);
    }



    //    条件分页查询
    //    page 当前页 limit每页显示条数
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result findPageQueryRole(@PathVariable Long page, @PathVariable Long limit,
                                    String userName) {
        Page<Object> userPage = new Page<>(page, limit);//page是当前页码，limit为每页显示的数据条数

        IPage<Users> userIPage = userService.selectPage(userPage, userName);
        System.out.println(userIPage);
        return Result.ok(userIPage);
    }

    //    添加角色
    @ApiOperation(value = "新增角色")
    @PostMapping("/save")
    public Result save(@RequestBody Users user) {
        boolean save = userService.save(user);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }

    }

    //修改角色
    @ApiOperation(value = "修改角色")
    @PutMapping("/update")
    public Result updateById(@RequestBody Users user) {
        boolean update = userService.updateById(user);
        if (update) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
    //    id删除
    @ApiOperation(value = "Id删除角色")
    @DeleteMapping("/remove/{id}")
    public Result removeRole(@PathVariable long id) {
        boolean remove = userService.removeById(id);
        if (remove == true) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
    //  id查询角色
    @ApiOperation(value = "获取角色")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        Users role = userService.getById(id);
        return Result.ok(role);
    }



    //批量删除角色
    @ApiOperation(value = "根据Id批量删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        userService.removeByIds(idList);
        return Result.ok();
    }
}
